package com.zmn.plat.admin.controller.company;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.DictModel;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.common.dto.organize.OrganizeDRO;
import com.zmn.plat.common.constant.CompanyConsts;
import com.zmn.plat.common.dictionary.CompanyDict;
import com.zmn.plat.model.entity.area.BaseArea;
import com.zmn.plat.model.entity.company.BaseCompany;
import com.zmn.plat.model.entity.company.BaseCompanyQuery;
import com.zmn.plat.model.vo.company.BaseCompanyVO;
import com.zmn.plat.services.interfaces.area.BaseAreaService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static java.util.stream.Collectors.toList;

/**
 * @author duanzuocai
 * @version v1.0
 * @modifier tanbiao 2019/09/06
 * @类说明：公司列表页、详情页
 * @since 2018/10/16 11:25
 */
@Controller
@RequestMapping("/company/")
public class CompanyListController extends CompanyBaseController {
    @Resource
    protected BaseAreaService areaService;

    @Resource
    BaseAreaService baseAreaService;

    /**
     * region 公司列表
     *
     * @param query
     * @return
     * @modifier tanbiao 2019/09/06 增加查询条件
     */
    @RequestMapping(value = "list")
    public ModelAndView list(@ModelAttribute BaseCompanyQuery query) {
        if (StringUtil.isNotBlank(query.getTimeRange())) {
            String[] timeRangeArray = query.getTimeRange().split("-");
            query.setStartCreateTime(timeRangeArray[0].trim() + " 00:00:00");
            query.setEndCreateTime(timeRangeArray[1].trim() + " 23:59:59");
        }
        List<BaseCompanyVO> items = companyService.listPageByQuery(query, getMcStaff().getStaffId());

        ModelAndView mav = new ModelAndView("company/list");

        // 省
        List<BaseArea> provinceAreas = baseAreaService.listByParentId(GlobalConsts.TOP_ID);
        List<VtDTO> provinces = provinceAreas.stream().map(mm -> new VtDTO(mm.getAreaId(), mm.getName(), true)).collect(toList());
        // 市
        if (query.getProvinceId() != null && query.getProvinceId() > 0) {
            List<BaseArea> citys = baseAreaService.listByParentId(query.getProvinceId());
            mav.addObject("citys", citys.stream().map(mm -> new VtDTO(mm.getAreaId(), mm.getName(), true)).collect(toList()));
        } else {
            mav.addObject("citys", new ArrayList<>());
        }
        // 区
        if (query.getCityId() != null && query.getCityId() > 0) {
            List<BaseArea> citys = baseAreaService.listByParentId(query.getCityId());
            mav.addObject("countys", citys.stream().map(mm -> new VtDTO(mm.getAreaId(), mm.getName(), true)).collect(toList()));
        } else {
            mav.addObject("countys", new ArrayList<>());
        }

        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("types", CompanyDict.getOrgTypeList());
        mav.addObject("statuses", GlobalDict.getStatusList(false));
        mav.addObject("provinces", provinces);
        mav.addObject("platTypeList", GlobalConsts.getPlatList());
        return mav;
    }

    /**
     * region 公司详情
     *
     * @param companyId
     * @return
     * @modifier tanbiao 2019/09/06 新增tab显示标识
     */
    @RequestMapping("detail")
    public ModelAndView detail(@RequestParam(value = "companyId", defaultValue = "0") int companyId,
        @RequestParam(value = "flag", defaultValue = "1") int flag) {
        BaseCompany company = companyId > 0 ? companyService.findByKey(companyId) : new BaseCompany();
        int sort = 5000;
        List<DictModel> dictModelList = CompanyDict.getOrgTypeList();
        if (Objects.isNull(company.getCompanyId())) {
            company.setSort(sort);
            dictModelList = dictModelList.stream().filter(e->!Objects.equals(e.getKey(),CompanyConsts.COMPANY_TYPE_SERVICE)).collect(toList());
        }
        ModelAndView mav = new ModelAndView("company/detail");

        List<BaseArea> province = areaService.listByParentId(GlobalConsts.TOP_ID);
        List<BaseArea> city = areaService.listByParentId(company.getProvinceId());
        List<BaseArea> district = areaService.listByParentId(company.getCityId());


        // 加载所有一级分类
        ResponseDTO<List<OrganizeDRO>> data = organizeListRemoteService.listByParentId(GlobalConsts.TOP_ID);
        if (data.isSuccess()) {
            mav.addObject("organOneList", data.getData());
        }

        // 如果存在一级节点，加载二级
        if (!NumberUtil.isNullOrZero(company.getPlat())) {
            ResponseDTO<List<OrganizeDRO>> dataLevelTwo = organizeListRemoteService.listByParentId(company.getPlat());
            if (dataLevelTwo.isSuccess()) {
                mav.addObject("organTwoList", dataLevelTwo.getData());
            }
        }

        // 如果存在二级节点，加载三级
        if (!NumberUtil.isNullOrZero(company.getOrgTwoId())) {
            ResponseDTO<List<OrganizeDRO>> dataLevelThr = organizeListRemoteService.listByParentId(company.getOrgTwoId());
            if (dataLevelThr.isSuccess()) {
                mav.addObject("organThrList", dataLevelThr.getData());
            }
        }

        // 公司类型存在子类型的需要加载出来
        if (!NumberUtil.isNullOrZero(company.getType())) {
            mav.addObject("sonTypes", CompanyDict.listOrgTypeByParent(company.getType()));
        }
        mav.addObject("provinces", province);
        mav.addObject("citys", city);
        mav.addObject("districts", district);
        mav.addObject("company", company);
        mav.addObject("types", dictModelList);
        mav.addObject("statuses", GlobalDict.getStatusList(false));
        mav.addObject("flag", flag);
        return mav;
    }

    /**
     * 公司资料
     *
     * @param companyId
     * @return
     * @author tanbiao 2019/09/06
     */
    @RequestMapping("companyMaterial")
    public ModelAndView companyMaterial(@RequestParam(value = "companyId", defaultValue = "0") int companyId,
        @RequestParam(value = "flag", defaultValue = "1") int flag) {
        BaseCompany company = companyId > 0 ? companyService.findByKey(companyId) : new BaseCompany();
        int sort = 5000;
        if (Objects.isNull(company.getCompanyId())) {
            company.setSort(sort);
        }
        ModelAndView mav = new ModelAndView("company/companyMaterial");
        mav.addObject("company", company);
        mav.addObject("flag", flag);
        return mav;
    }

    /**
     * 公司资料
     *
     * @param companyId
     * @return
     * @author tanbiao 2019/09/06
     */
    @RequestMapping("companyFinance")
    public ModelAndView companyFinance(@RequestParam(value = "companyId", defaultValue = "0") int companyId,
        @RequestParam(value = "flag", defaultValue = "1") int flag) {
        BaseCompany company = companyId > 0 ? companyService.findByKey(companyId) : new BaseCompany();
        int sort = 5000;
        if (Objects.isNull(company.getCompanyId())) {
            company.setSort(sort);
        }
        ModelAndView mav = new ModelAndView("company/companyFinance");
        mav.addObject("company", company);
        mav.addObject("flag", flag);
        return mav;
    }
}
